package com.intellagric.agriecom.service.agriecom_news;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.agriecom.module.agriecom_news.NewsResult;
import com.intellagric.agriecom.module.agriecom_news.NewsService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.AgriecomNewsMapper;
import com.intellagric.mapper.AgriecomNewsTypeMapper;

import com.intellagric.module.agriecom.agriecom_news.NewsVo;
import com.intellagric.pojo.AgriecomNews;
import com.intellagric.pojo.AgriecomNewsExample;
import com.intellagric.pojo.AgriecomNewsType;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private AgriecomNewsMapper newsDao;

    @Autowired
    private AgriecomNewsTypeMapper newsTypeDao;

    /**
     * 通过id查找
     * @param id
     * @return news
     */
    @Override
    public AgriecomNews selectNewsById(String id){
        AgriecomNews news=null;
        news=newsDao.selectByPrimaryKey(id);
        return news;
    }

    /**
     * 插入一条数据
     * @param news
     * @return msg
     */
    @Override
    public ResponseMessage insertNews(AgriecomNews news) {

        int r=newsDao.insertSelective(news);
        if(r==0){
            return ResponseMessage.success("插入失败");
        }
        return ResponseMessage.success();
    }

    /**
     * 通过id删除
     * @param id
     * @return msg
     */
    @Override
    public ResponseMessage deleteNews(String id) {
        int r=newsDao.deleteByPrimaryKey(id);
        if(r==0)
            return ResponseMessage.fail("删除失败");
        return ResponseMessage.success();
    }

    /**
     * 修改信息
     * @param news
     * @return msg
     */
    @Override
    public ResponseMessage editNews(AgriecomNews news) {
        AgriecomNewsExample example=new AgriecomNewsExample();
        example.createCriteria().andIdEqualTo(news.getId());
        //设置更新时间
        news.setUpdateDate(new Date(System.currentTimeMillis()));
        int r=newsDao.updateByExampleSelective(news,example);
        if(r==0)
            return ResponseMessage.fail();
        return ResponseMessage.success();
    }

    /**
     * 获取所有新闻
     * @return
     */
    @Override
    public List<AgriecomNews> getAll(){
        AgriecomNewsExample example=new AgriecomNewsExample();
        //example.setOrderByClause("sort desc");
        example.createCriteria().andIdIsNotNull();
        return newsDao.selectByExampleWithBLOBs(example);
    }
    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    @Override
    public LayuiDataGridResult getList(int page, int rows) {
        PageHelper.startPage(page,rows);
        AgriecomNewsExample example=new AgriecomNewsExample();
        example.createCriteria().andIdIsNotNull();
        List<AgriecomNews> list=newsDao.selectByExampleWithBLOBs(example);
        //System.out.println("ss"+list.get(0).getNewsDetail());
        ArrayList<NewsResult> resultList=new ArrayList<>();
        for (AgriecomNews news:list) {
            NewsResult result=new NewsResult(news);
            //设置新闻种类名字
            AgriecomNewsType newsType=newsTypeDao.selectByPrimaryKey(news.getNewsTypeId());
            if(newsType!=null)//如果等于null就说明数据库未设置新闻类型
                result.setNewsTypeName(newsType.getName());
            resultList.add(result);
        }
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(resultList,this.getAll().size());
        lay.setCode(0);
        lay.setMsg("查询成功");
        return lay;
    }

    @Override
    public LayuiDataGridResult getListByType(String newsTypeId,int page,int limit){
        PageHelper.startPage(page,limit);
        AgriecomNewsExample example=new AgriecomNewsExample();
        example.createCriteria().andNewsTypeIdEqualTo(newsTypeId);
        List<AgriecomNews> resultList=newsDao.selectByExampleWithBLOBs(example);
        PageInfo<AgriecomNews> info=new PageInfo<>(resultList);
        return LayuiDataGridResult.success().add(resultList,info.getSize());
    }

    /**
     * 排序获取近期热门新闻
     * @param page
     * @param rows
     * @param vo
     * @return
     */
    @Override
    public LayuiDataGridResult getHotListByVo(int page, int rows, NewsVo vo){
        PageHelper.startPage(page,rows);
        AgriecomNewsExample example=new AgriecomNewsExample();
        //热门新闻
        example.createCriteria().andIsHotEqualTo("1");
        String orderClause="";
        if(vo.isReadCount()){
            orderClause+=",read_count desc";
        }
        if(vo.isThumbsUpCount()){
            orderClause+=",thumbs_up_count desc";
        }
        if(vo.isCommentCount()){
            orderClause+=",comment_count desc";
        }

        if(vo.isShareCount()){
            orderClause+=",share_count desc";
        }

        if(vo.isCreateDate()){
            orderClause+=",create_date desc";
        }

        if(vo.isUpdateDate()){
            orderClause+=",update_date desc";
        }
        if(!orderClause.equals("")){
            orderClause=orderClause.substring(1);
        }
        example.setOrderByClause(orderClause);
        List<AgriecomNews> list = newsDao.selectByExample(example);
        PageInfo<AgriecomNews> pageInfo=new PageInfo<>(list);
        return LayuiDataGridResult.success().add(list,pageInfo.getTotal());
    }

    /**
     * 只按时间排序
     * @return
     */
    public List<AgriecomNews> getLatestNews(int page,int limit){
        PageHelper.startPage(page,limit);
        AgriecomNewsExample example= new AgriecomNewsExample();
        example.setOrderByClause("create_date desc");
        return newsDao.selectByExample(example);

    }


    @Override
    public LayuiDataGridResult getNewsListByKeyword(int page, int rows, String keyword) {
        PageHelper.startPage(page, rows);

        AgriecomNewsExample agriecomNewsExample=new AgriecomNewsExample();

        agriecomNewsExample.createCriteria().andTitleLike("%"+keyword+"%");

        List<AgriecomNews> newsList=newsDao.selectByExampleWithBLOBs(agriecomNewsExample);

        return LayuiDataGridResult.success().add(newsList,newsList.size() );

    }


}
